# vi: ts=4

from etgui import *
import CmdNo,string,sys
from PageLayout import *
from FileDialog import ShowFileDialog

class SimpleTextEditor:
	def __init__(self,model):
		self.model=model
		self.model.SetValue("Text",'')
		self.model.SetValue("__cancel__",0)
		self.originput=''
		self.SetText('')
	def GetButtons(self):
		return [
			ActionButton(CmdNo.cIdOk, "Ok",action=self.Ok),
			ActionButton(CmdNo.cIdCancel, "Cancel",action=self.Cancel)
		]
	def GetTitle(self):
		return self.__class__.__name__
	def GetText(self):
		return self.model.GetValue("Text")
	def SetText(self,input):
		self.originput=input
		self.model.SetValue("Text",input)
	def SetDialog(self, dialog):
		self.dialog = dialog
	def Close(self):
		self.dialog.Close()
	def Ok(self):
		self.Close()
	def Cancel(self):
		self.model.SetValue("__cancel__", 1)
		self.Close()
	def IsCancelled(self):
		return self.model.GetValue("__cancel__")
	def GetModel(self):
		return self.model
	def ShowUnderMouse(self):
		self.dialog.ShowUnderMouse()
		if self.IsCancelled():
			return CmdNo.cIdCancel
		return CmdNo.cIdOk

class TextFileEditor(SimpleTextEditor):
	def __init__(self, model):
		SimpleTextEditor.__init__(self,model)
		self.file=''
		self.Init()
	def Init(self):
		self.Load()
	def GetButtons(self):
		return [
			ActionButton("New",default=1,action=self.New),
			ActionButton("Load...",default=1,action=self.Load),
			ActionButton("Reload",default=1,action=self.Reload),
			ActionButton("Save",default=1,action=self.Save),
			ActionButton("Save As...",default=1,action=self.SaveAs),
		]
	def SetFile(self,file):
		if file and gSystem.DirName(file):
			gSystem.ChangeDirectory(gSystem.DirName(file))
		self.file=file
	def New(self):
		if not self.SavedChanges():
			return
		self.SetText('')
		self.file=''
	def Reload(self):
		if not self.SavedChanges():
			return
		if self.file:
			self.LoadFile(self.file)
		else:
			self.Load()
	def Load(self):
		if not self.SavedChanges():
			return
		file=ShowFileDialog(eFDRead,self.file,"Load File")
		self.LoadFile(file)
	def LoadFile(self,file):
		if file:
			try:
				input=open(file).read()
			except IOError,details:
				ShowAlert(eAlertStop,"Error reading file @B%s@B:\n%s" %(file,details[1]))
				return
			self.SetText(input)
			self.SetFile(file)
	def SavedChanges(self):
		if self.file and self.GetText()!=self.originput:
			file=self.file
			if not file:
				file="?Untitled?"
			result=ShowAlert(eAlertCaution,"Save changes to @b%s@b?"%file)
			if result==CmdNo.cIdYes:
				return self.Save()
			elif result==CmdNo.cIdCancel:
				return 0
		return 1
	def Save(self):
		if self.file:
			return self.SaveFile(self.file)
		else:
			return self.SaveAs()
	def SaveAs(self):
		file=ShowFileDialog(eFDWrite,self.file,"Save File")
		return self.SaveFile(file)
	def SaveFile(self,file):
		if file:
			try:
				fp=open(file,'w')
				fp.write(self.GetText())
				fp.close()
				self.SetFile(file)
				self.originput=self.GetText()
				return 1
			except IOError,details:
				ShowAlert(eAlertStop,"Error writing file @B%s@B:\n%s" %(file,details[1]))

def MakeEditorDialog(handler=SimpleTextEditor, title='', flags=eWinDefault):
	model=PotGuiModel()
	textfield=TextView(gFixedFont)
	text = PotGuiTextView("Text",textfield,Point(600))
	editor=handler(model)
	panel=VBox(
		align=eVObjHExpand|eVObjVExpand,
		items=[
			text,
			HBox(
				align=eVObjVBase|eVObjHEqual|eVObjHExpand,
				items= editor.GetButtons()
			)
		]
	)
	if not title:
		title = editor.GetTitle()
	if not title:
		title = "Text Editor"
	dialog=PotGuiDialog(model, panel, title, flags)
	editor.SetDialog(dialog)
	return editor

gEditor=None
def Show():
	global gEditor
	if not gEditor:
		gEditor=MakeEditorDialog(SimpleTextEditor)
	model = gEditor.GetModel()
	model.SetValue("Text","foo\nbar\n")
	gEditor.ShowUnderMouse()

